﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.IO;

namespace Botwave.Log
{
	public static class Logger
    {
        #region 属性
        private static ILog Text;
        private static ILog Database;
        private static ILog Both;
        private static Writer DefaultWriter;
        #endregion

        #region 构造函数
        static Logger()
        {
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(System.Web.HttpContext.Current.Server.MapPath("~") + @"Config/log4net.config"));
            Text = LogManager.GetLogger("Text");
            Database = LogManager.GetLogger("Database");
            Both = LogManager.GetLogger("Both");
            DefaultWriter = Writer.Text;
        }
        #endregion

        #region 公共方法

        #region 默认写日志方式
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="description">描述信息</param>
        public static void Write(string description)
        {
            Write("Root", description);
        }
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="operation">操作类型名称</param>
        /// <param name="description">描述信息</param>
        public static void Write(string operation, string description)
        {
            Write(operation, "正常操作", description);
        }
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="operation">操作类型名称</param>
        /// <param name="exception">异常类型名称</param>
        /// <param name="description">描述信息</param>
        public static void Write(string operation, string exception, string description)
        {
            Write(null, operation, exception, description);
        }
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="userName">操作员</param>
        /// <param name="operation">操作类型名称</param>
        /// <param name="exception">异常类型名称</param>
        /// <param name="description">描述信息</param>
        public static void Write(string userName, string operation, string exception, string description)
        {
            Write(DateTime.Now, userName, operation, exception, description);
        }
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="startDT">开始时间</param>
        /// <param name="userName">操作员</param>
        /// <param name="operation">操作类型名称</param>
        /// <param name="exception">异常类型名称</param>
        /// <param name="description">描述信息</param>
        public static void Write(DateTime startDT, string userName, string operation, string exception, string description)
        {
            switch (DefaultWriter)
            {
                case Writer.Text: Text.Info(GetOperationLog(startDT, userName, operation, exception, description)); break;
                case Writer.Database: Database.Info(GetOperationLog(startDT, userName, operation, exception, description)); break;
                case Writer.Both: Both.Info(GetOperationLog(startDT, userName, operation, exception, description)); break;
                default: Text.Info(GetOperationLog(startDT, userName, operation, exception, description)); break;
            }

        }
        #endregion

        #region 写文本日志
        /// <summary>
        /// 写文本日志
        /// </summary>
        /// <param name="description">描述信息</param>
        public static void WriteText(string description)
        {
            WriteText("Root", description);
        }
        /// <summary>
        /// 写文本日志
        /// </summary>
        /// <param name="operation">操作类型名称</param>
        /// <param name="description">描述信息</param>
        public static void WriteText(string operation, string description)
        {
            WriteText(operation, "正常操作", description);
        }
        /// <summary>
        /// 写文本日志
        /// </summary>
        /// <param name="operation">操作类型名称</param>
        /// <param name="exception">异常类型名称</param>
        /// <param name="description">描述信息</param>
        public static void WriteText(string operation, string exception, string description)
        {
            WriteText(null, operation, exception, description);
        }
        /// <summary>
        /// 写文本日志
        /// </summary>
        /// <param name="userName">操作员</param>
        /// <param name="operation">操作类型名称</param>
        /// <param name="exception">异常类型名称</param>
        /// <param name="description">描述信息</param>
        public static void WriteText(string userName, string operation, string exception, string description)
        {
            WriteText(DateTime.Now, userName, operation, exception, description);
        }
        /// <summary>
        /// 写文本日志
        /// </summary>
        /// <param name="startDT">开始时间</param>
        /// <param name="userName">操作员</param>
        /// <param name="operation">操作类型名称</param>
        /// <param name="exception">异常类型名称</param>
        /// <param name="description">描述信息</param>
        public static void WriteText(DateTime startDT, string userName, string operation, string exception, string description)
        {
            Text.Info(GetOperationLog(startDT, userName, operation, exception, description));
        }
        #endregion

        #region 写数据日志
        /// <summary>
        /// 写数据库日志
        /// </summary>
        /// <param name="description">描述信息</param>
        public static void WriteInDB(string description)
        {
            WriteInDB("Root", description);
        }
        /// <summary>
        /// 写数据库日志
        /// </summary>
        /// <param name="operation">操作类型名称</param>
        /// <param name="description">描述信息</param>
        public static void WriteInDB(string operation, string description)
        {
            WriteInDB(operation, "正常操作", description);
        }
        /// <summary>
        /// 写数据库日志
        /// </summary>
        /// <param name="operation">操作类型名称</param>
        /// <param name="exception">异常类型名称</param>
        /// <param name="description">描述信息</param>
        public static void WriteInDB(string operation, string exception, string description)
        {
            WriteInDB(null, operation, exception, description);
        }
        /// <summary>
        /// 写数据库日志
        /// </summary>
        /// <param name="userName">操作员</param>
        /// <param name="operation">操作类型名称</param>
        /// <param name="exception">异常类型名称</param>
        /// <param name="description">描述信息</param>
        public static void WriteInDB(string userName, string operation, string exception, string description)
        {
            WriteInDB(DateTime.Now, userName, operation, exception, description);
        }
        /// <summary>
        /// 写数据库日志
        /// </summary>
        /// <param name="startDT">开始时间</param>
        /// <param name="userName">操作员</param>
        /// <param name="operation">操作类型名称</param>
        /// <param name="exception">异常类型名称</param>
        /// <param name="description">描述信息</param>
        public static void WriteInDB(DateTime startDT, string userName, string operation, string exception, string description)
        {
            Database.Info(GetOperationLog(startDT, userName, operation, exception, description));
        }
        #endregion

        #region 写文本和数据日志
        /// <summary>
        /// 写文本和数据日志
        /// </summary>
        /// <param name="description">描述信息</param>
        public static void WriteBoth(string description)
        {
            WriteBoth("Root", description);
        }
        /// <summary>
        /// 写文本和数据日志
        /// </summary>
        /// <param name="operation">操作类型名称</param>
        /// <param name="description">描述信息</param>
        public static void WriteBoth(string operation, string description)
        {
            WriteBoth(operation, "正常操作", description);
        }
        /// <summary>
        /// 写文本和数据日志
        /// </summary>
        /// <param name="operation">操作类型名称</param>
        /// <param name="exception">异常类型名称</param>
        /// <param name="description">描述信息</param>
        public static void WriteBoth(string operation, string exception, string description)
        {
            WriteBoth(null, operation, exception, description);
        }
        /// <summary>
        /// 写文本和数据日志
        /// </summary>
        /// <param name="userName">操作员</param>
        /// <param name="operation">操作类型名称</param>
        /// <param name="exception">异常类型名称</param>
        /// <param name="description">描述信息</param>
        public static void WriteBoth(string userName, string operation, string exception, string description)
        {
            WriteBoth(DateTime.Now, userName, operation, exception, description);
        }
        /// <summary>
        /// 写文本和数据日志
        /// </summary>
        /// <param name="startDT">开始时间</param>
        /// <param name="userName">操作员</param>
        /// <param name="operation">操作类型名称</param>
        /// <param name="exception">异常类型名称</param>
        /// <param name="description">描述信息</param>
        public static void WriteBoth(DateTime startDT, string userName, string operation, string exception, string description)
        {
            Database.Info(GetOperationLog(startDT, userName, operation, exception, description));
        }
        #endregion

        #endregion

        #region 私有方法
        private static OperationLog GetOperationLog(DateTime startDT, string userName, string operation, string exception, string description)
        {
            OperationLog OL = new OperationLog();
            OL.OpStartTime = startDT;
            if (!string.IsNullOrEmpty(userName)) { OL.PortalID = userName; }
            OL.OperationName = operation;
            OL.ExceptionName = exception;
            OL.Description = description;
            return OL;
        }
        #endregion
    }
    #region 枚举
    public enum Writer
    {
        Text,
        Database,
        Both
    }
    #endregion
}
